import 'package:flutter/material.dart';

// 入口 - 界面基础结构
class FormMain extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Theme(
        data: ThemeData(primaryColor: Colors.pink),
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("From 表单"),
          ),
          body: Container(
            padding: EdgeInsets.all(32.0),
            child: FormDemo(),
          ),
        ));
  }
}

// 输入框 - 示例代码
class TextFieldDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.all(16.0),
      child: TextField(
        decoration: InputDecoration(
            //   border: OutlineInputBorder(
            //     borderSide: BorderSide(
            //       width: 10.0,
            //       color: Colors.yellow,
            //     ),
            //   ),
            ),
      ),
    );
  }
}

// 表单
class FormDemo extends StatelessWidget {
// 输入框控制器
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

// 提交信息
  void _submitBtn() {
    // print("用户名：${_nameController.text}  密码：${_passwordController.text}");
    _formKey.currentState.validate();
  }

  String _passwordValidator(value) {
    if (value == '123') {
      return "参数错误";
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              controller: _nameController,
              decoration: InputDecoration(
                  labelText: '用户名',
                  icon: Icon(Icons.person),
                  border: OutlineInputBorder()),
            ),
            SizedBox(height: 16.0),
            TextFormField(
              controller: _passwordController,
              decoration: InputDecoration(
                labelText: '密码',
                icon: Icon(Icons.keyboard),
                border: OutlineInputBorder(),
              ),
              validator: _passwordValidator,
            ),
            SizedBox(height: 32.0),
            SizedBox(
              width: double.infinity,
              child: RaisedButton(
                child: Text("登录", style: TextStyle(color: Colors.white)),
                color: Colors.pink,
                onPressed: _submitBtn,
              ),
            ),
          ],
        ));
  }
}
